<?php

namespace App\Admin\Controllers;

use App\model\Post;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class PostController extends AdminController
{
    protected $states = [
        'on'  => ['value' => 0, 'text' => '显示', 'color' => 'success'],
        'off' => ['value' => 1, 'text' => '隐藏', 'color' => 'danger'],
    ];
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '新闻资讯';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Post());

        $grid->column('id', __('Id'));
        $grid->column('title', __('标题'));

        $grid->column('thumbnail', __('缩略图'))->image('',100,100);
        $grid->column('desc', __('描述'));
        $grid->column('sort', __('排序'))->editable();
        $grid->column('is_display', __('是否显示'))->switch($this->states);
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时间'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Post::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('title', __('Title'));
        $show->field('logo', __('Logo'));
        $show->field('desc', __('Desc'));
        $show->field('thumbnail', __('Thumbnail'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Post());

        $form->text('title', __('标题'))->required();
        $form->number('sort','排序')->default(0)->help('排序数字越大，资讯越靠前');
        $form->textarea('desc', __('描述'))->required()->help('最多35个字');
        $form->cropper('thumbnail', __('缩略图'))->cRatio(390,560)->uniqueName();
        $form->UEditor('content', __('内容'));
        $form->switch('is_display','是否显示')->states($this->states);

        return $form;
    }
}
